﻿/* Write a program that finds how many times a substring is contained in a given text (perform case insensitive search).
		Example: The target substring is "in". The text is as follows:
            text = "We are livINg IN an yellow submarine. We don't have anythIng else. 
                   Inside the submarine is very tight. 
                   So we are drinking all day. 
                   We will move out of It in 5 days";
The result is: 9. */

using System;
using System.Text.RegularExpressions;

namespace _4.SubstringOccurance
{
    class SubstringOccurance
    {
        static void Main()
        {
            Console.Write("Target substring: ");
            string s = Console.ReadLine();

            Console.Write("Text to search in: ");
            string text = Console.ReadLine();

            //text = "We are livINg IN an yellow submarine. We don\'t have anythIng else. Inside the submarine is very tight. So we are drinking all day. We will move out of It in 5 days";
            string regexpattern = s + "+";
            Regex r = new Regex(@regexpattern, RegexOptions.IgnoreCase);
            int matches = 0;
            foreach (Match ItemMatch in r.Matches(text))
            {
                Console.WriteLine(ItemMatch.ToString());
                matches++;
            }
            Console.WriteLine("Found {0} occurances of {1}", matches, s);
        }
    }
}
